using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using TanashCMS.Business;
using TanashCMS.Business.DTO;
using TanashCMS.Business.Task;

public partial class manager_inventory_CustomiseCategory : CMSManagementPageBase
{
    protected int _categoryId { get { return Request.QueryString["id"] != null ? int.Parse(Request.QueryString["id"]) : 0; } }
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public override void DataBind()
    {
        Category cat = Category.Get(_categoryId);

        // load master page list
        DirectoryInfo dir = new DirectoryInfo(System.Web.Hosting.HostingEnvironment.MapPath(mWebsite.TemplatesFolder));
        WebpageTemplate[] templates = mWebsite.GetTemplates();
        if (templates.Length == 0)
        {
            ListItem item = new ListItem();
            item.Text = "[no templates found]";
            item.Value = "";
            item.Selected = true;
            ddlMasterPage.Items.Add(item);
        }

        foreach (WebpageTemplate template in templates)
        {
            ListItem item = new ListItem();
            item.Text = template.TemplateName;
            item.Value = template.TemplateName;
            ddlMasterPage.Items.Add(item);
        }

        categoryImage.ImageUrl = "/services/thumbnail.aspx?method=restricttosize&sx=400&i=" + mWebsite.ImagesFolder + cat.PhotoURL;
        hiddenImageUrl.Value = mWebsite.ImagesFolder + cat.PhotoURL;

        litHeading.Text = "Customise " + System.Web.HttpUtility.HtmlDecode(cat.Title);

        base.DataBind();
    }

    protected override void OnInit(EventArgs e)
    {
        Session["FCKeditor:UserFilesPath"] = mWebsite.ImagesFolder;
        
        if (Request.QueryString["id"] != null && !IsPostBack)
            DataBind();


        base.OnInit(e);
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        string photoUrl = hiddenImageUrl.Value;
        photoUrl = photoUrl.Replace(mWebsite.ImagesFolder, "");

        Category cat = Category.Get(_categoryId);
        new InventoryTask(mWebsite.ID).UpdateCategory(_categoryId, cat.ParentID, cat.Title, fckContent.Value, photoUrl, ddlMasterPage.SelectedValue, User.Identity.Name);

        if (!mWebsite.NeedsPublish)
        {
            mWebsite.NeedsPublish = true;
            Configuration.GetCurrentSession().SaveOrUpdate(mWebsite);
            Configuration.GetCurrentSession().Flush();
        }

        DataBind();

        ShowSystemFeedback("Category data has been saved.");
    }
}
